import com.gitee.feizns.data.MetaData;
import com.gitee.feizns.data.impl.MetaDataImpl;
import com.gitee.feizns.domain.CustomizeConfig;
import lombok.SneakyThrows;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.FileOutputStream;
import java.io.StringWriter;

/**
 * @author feizns
 * @since 2019/7/7
 */
public class Controller {

    public static void main(String[] args) {
        VelocityEngine engine = new VelocityEngine();

        engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        engine.init();

        Template template = engine.getTemplate("Controller.vm", "UTF-8");

        CustomizeConfig customizeConfig = new CustomizeConfig();
        customizeConfig.setAuthor("feizns");
        customizeConfig.setBasePackage("com.gitee.feizns");
        customizeConfig.setDate("2019-07-07");

        MetaData metaData = new MetaDataImpl("jdbc:mysql://116.62.134.108/yx_test?useSSL=false", "root", "feizns123", "com.mysql.jdbc.Driver");

        VelocityContext vc = new VelocityContext();
        vc.put("utils", new TestPojo.Utils());
        vc.put("customize", customizeConfig);
        vc.put("table", metaData.getTables().get(3));
        StringWriter sw = new StringWriter();

        template.merge(vc, sw);

//        write(sw);

        System.out.println(sw);

    }

    @SneakyThrows
    private static final void write(StringWriter stringWriter) {
//        stringWriter.
        FileOutputStream stream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\AdminController.java");
        stream.write(stringWriter.toString().getBytes());
        stream.flush();
    }

}
